package com.zhouzi.zhaoyang.ui.activity

import android.content.Intent
import android.os.Build
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import cn.jpush.android.api.JPushInterface
import com.zhouzi.zhaoyang.R
import com.zhouzi.zhaoyang.base.BaseActivity
import com.zhouzi.zhaoyang.bean.LoginBean
import com.zhouzi.zhaoyang.config.SharedPreferencesFinal
import com.zhouzi.zhaoyang.mvp.p.LoginActivityPresenter
import com.zhouzi.zhaoyang.mvp.v.ILoginActivityView
import com.zhouzi.zhaoyang.util.DownLoadApkUtil
import com.zhouzi.zhaoyang.util.SPUtils
import kotlinx.android.synthetic.main.activity_login.*

/**
 * Created by ZhouZi on 2019/10/11.
 * time:9:42
 * ----------Dragon be here!----------/
 * 　　　┏┓　　 ┏┓
 * 　　┏┛┻━━━┛┻┓━━━
 * 　　┃　　　　　 ┃
 * 　　┃　　　━　  ┃
 * 　　┃　┳┛　┗┳
 * 　　┃　　　　　 ┃
 * 　　┃　　　┻　  ┃
 * 　　┃　　　　   ┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛━━━━━
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━━━━━━神兽出没━━━━━━━━━━━━━━
 */
class LoginActivity : BaseActivity(), ILoginActivityView {

    override fun errorToken() {
    }

    override fun newestSuccess(
        currentVersion: String,
        data: com.zhouzi.zhaoyang.bean.VersionCodeBean?
    ) {
        data?.let {
            DownLoadApkUtil.update(data)
        }
    }


    override fun newestFailed(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT)
    }

    private val p = LoginActivityPresenter()

    init {
        p.attachView(this)
    }

    override fun showLoadDialog() {
        loadDialog.show()
    }

    override fun dismissLoadDialog() {
        loadDialog.dismiss()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initView() {


//        CrashReport.testJavaCrash()
        val userName = SPUtils.getInstance().getValue(SharedPreferencesFinal.USER_NAME, "") as String
        etLoginAccount.setText(userName)
        viewClick()
        if (DownLoadApkUtil.getVersionName(this) != "-1") {
            p.newest(DownLoadApkUtil.getVersionName(this).toString())
        }

    }

    var id: String = ""
    override fun onStart() {
        super.onStart()
        // 删除别名
        id = SPUtils.getInstance().getValue(SharedPreferencesFinal.USER, "") as String
        if(!TextUtils.isEmpty(id)){
            JPushInterface.deleteAlias(mContext, id.toInt())
            JPushInterface.getAlias(mContext,id.toInt())

        }

//                id = SPUtils.getInstance().getValue(SharedPreferencesFinal.USER, "") as String
//        if(!TextUtils.isEmpty(id)   ){
//            JPushInterface.setAlias(mContext, id.toInt(),"null")
//            JPushInterface.getAlias(mContext,id.toInt())
//
//        }
    }

    private fun viewClick() {
        Log.i("sno", Build.SERIAL);
        tvLoginBtnEnter.setOnClickListener {
            val account = etLoginAccount.text.toString().trim()
            val pwd = etLoginPwd.text.toString().trim()
            if (account.isEmpty()) {
                Toast.makeText(mContext, "请输入账号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (pwd.isEmpty()) {
                Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val map = mutableMapOf(
                Pair("username", account),
                Pair("password", pwd),
                Pair("deviceNo", Build.SERIAL),
                Pair("registrationId", JPushInterface.getRegistrationID(this))
            )
            p.login(map)
        }
    }

    override fun loginSuccess(data: LoginBean?) {
        data?.let {
            SPUtils.getInstance().putValue(SharedPreferencesFinal.TOKEN, data.token!!)
            SPUtils.getInstance().putValue(SharedPreferencesFinal.USER_NAME, etLoginAccount.text.toString().trim())

            //设置别名
            if (!TextUtils.isEmpty(data.id)){
                JPushInterface.setAlias(mContext, data.id!!.toInt(), data.id)
                SPUtils.getInstance().putValue(SharedPreferencesFinal.USER, data.id)
            }


        }


        var intent = Intent(mContext, MainActivity::class.java)
        intent.putExtra("personInfo", data)
        startActivity(intent)
        finish()
    }

    override fun loginFailed(msg: String) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
    }

    override fun onDestroy() {
        super.onDestroy()
        p.detachView()
    }

}